﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class RackEntry : System.Web.UI.Page
    {
        private RackManager aManager = new RackManager();
        private Rack aRack = new Rack();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadGridview();
            }
            catch (Exception)
            {
                throw;
            } 
        }

        protected void SaveCompany(object sender, EventArgs e)
        {
            try
            {
                aManager = new RackManager();
                aRack.RackName = rackNameTextBox.Value;
                string message = aManager.SaveRack(aRack);
                messageResult.InnerText = message;
                Clear();
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Company name can not be saved" + "\n" + exception.ToString();
            }
        }

        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            rackIdTextBox.Value = "";
            rackNameTextBox.Value = "";
        }

        public void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int RackId = Convert.ToInt32(rackIdTextBox.Value);
                RackManager rackManager = new RackManager();
                string aa = rackManager.DeleteRack(RackId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        private void LoadGridview()
        {
            try
            {
                List<Rack> aRacks;
                aRacks = aManager.GetAllRack();
                rackGridView.DataSource = aRacks;
                rackGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadGridViewOnSaerch(int i, string rackIdorName)
        {
            try
            {
                rackGridView.DataSource = null;
                rackGridView.DataBind();
                List<Rack> racks;
                RackManager rackManager = new RackManager();
                racks = rackManager.GetAllRackByIdOrName(i, rackIdorName);
                rackGridView.DataSource = racks;
                rackGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void searchByIdButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            LoadGridViewOnSaerch(i, searchIdTextBox.Value);
        }
        protected void searchByNameButton_Click(object sender, EventArgs e)
        {
            int i = 1;
            LoadGridViewOnSaerch(i, searchNameTextBox.Value);
        }
        protected void rackGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            rackGridView.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

    }
}